package com.example.jixeishi1.controller;

import com.example.jixeishi1.dao.AddressMapper;
import com.example.jixeishi1.dao.CartMapper;
import com.example.jixeishi1.dao.OrderShopMapper;
import com.example.jixeishi1.entity.AddressEntity;
import com.example.jixeishi1.entity.CartEntity;
import com.example.jixeishi1.entity.OrderShopEntity;
import com.example.jixeishi1.entity.ProductEntity;
import com.example.jixeishi1.service.*;
import com.example.jixeishi1.vo.CartVo;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Slf4j
@Controller

public class IndexController {
    @Autowired
    private ProductService productService;
    @Autowired
    private CartService cartService;
    @Autowired
    private OrderShopService orderShopService;
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderShopMapper orderShopMapper;
    @Autowired
    HttpSession session;
    @GetMapping(value = "/index")
    public ModelAndView index() {
        List<CartEntity> shoppingEntities = new ArrayList<>();
        Double oneAll = 0.00;



        shoppingEntities = cartService.getShopping();
        List<CartVo> cartVos = new ArrayList<>();
        for(CartEntity cartEntity :shoppingEntities){
            CartVo cartVo = new CartVo();

            Integer num = cartEntity.getNum();
            Double money = cartEntity.getPricecommodity();
            oneAll = num * money;
            cartVo.setCommodityId(cartEntity.getCommodityId());
            cartVo.setNum(cartEntity.getNum());
            cartVo.setInTime(cartEntity.getInTime());
            cartVo.setPricecommodity(cartEntity.getPricecommodity());
            cartVo.setCmmodityName(cartEntity.getCmmodityName());
            cartVo.setPricecommodityAll(oneAll);
            cartVo.setId(cartEntity.getId());
            cartVo.setSubtitle(cartEntity.getSubtitle());
            cartVo.setMainImage(cartEntity.getMainImage());
            cartVos.add(cartVo);
        }
        Double all = 0.00;
        List<OrderShopEntity> orderShopEntities = orderShopMapper.getorderShopAll();
        for(OrderShopEntity orderShopEntity : orderShopEntities){
            if(ObjectUtils.isEmpty(orderShopEntity)){

            }else{
                Integer num = orderShopEntity.getNum();
                Double money = orderShopEntity.getPricecommodity();
                all += num * money;
            }
        }

        List<ProductEntity> productEntityList = new ArrayList<>();
        productEntityList = productService.getCommodityEntity();
        ModelAndView mav = new ModelAndView("st");
        orderShopService.getorderShopAll();
        mav.addObject("comds",productEntityList);
        int i =0;
        for(ProductEntity productEntity : productEntityList){
            mav.addObject("comds" + i , productEntity);
            i+=1;
        }
        mav.addObject("comds",productEntityList);
        System.out.println(productEntityList);
        mav.addObject("shoppings",cartVos);
        System.out.println(cartVos);

        mav.addObject("oneAll",oneAll);
        if(all ==0.00){
            mav.addObject("all",all);
            System.out.println("总价"+all);
        }else{
            mav.addObject("all",all);
            System.out.println("总价"+all);


        }
        session.setAttribute("allq",all);
        session.setAttribute("spxx",cartVos);
        System.out.println(all);
        return mav;
    }


    @GetMapping(value = "/pay")
    public ModelAndView pay() {


        ModelAndView mav = new ModelAndView("pay");
        List<OrderShopEntity> orderShopEntities = new ArrayList<>();
        orderShopEntities = orderShopService.getorderShopAll();
        List<CartVo> cartVos = new ArrayList<>();
        Integer i = 0;
        Integer index = 0;
        List<CartEntity> shoppingEntities = new ArrayList<>();
        List<AddressEntity> addressEntities = addressMapper.getListAddressByUid();
        mav.addObject("addresslist" , addressEntities);
        AddressEntity address = new AddressEntity();
        if(CollectionUtils.isEmpty(addressEntities)){

        }else{
            address  = addressEntities.get(0);
        }
        mav.addObject("address1" , address);
        Double all = 0.00;
        if(orderShopEntities.size()!=0 && orderShopEntities!=null){
            for(OrderShopEntity orderShopEntity:orderShopEntities){
                if(orderShopEntity.getId().equals(200)){
                    shoppingEntities = cartMapper.getShoppingInfo();
                    i = 1;
                }
            }
        }
        if(i.equals(1)){
            for(CartEntity cartEntity :shoppingEntities) {
                CartVo cartVo = new CartVo();
                Integer num = cartEntity.getNum();
                index +=num;
                Double oneAll = num * cartEntity.getPricecommodity();
                cartVo.setPricecommodityAll(oneAll);
                cartVo.setCmmodityName(cartEntity.getCmmodityName());
                cartVo.setPricecommodity(cartEntity.getPricecommodity());
                cartVo.setInTime(cartEntity.getInTime());
                cartVo.setNum(cartEntity.getNum());
                cartVo.setCommodityId(cartEntity.getCommodityId());
                cartVo.setSubtitle(cartEntity.getSubtitle());
                cartVo.setMainImage(cartEntity.getMainImage());
                cartVos.add(cartVo);
                mav.addObject("ordershop", cartVos);
                all += oneAll;
            }
        }else if(i.equals(0)){
            for(OrderShopEntity orderShopEntity :orderShopEntities){
                CartVo cartVo = new CartVo();
                Integer num = orderShopEntity.getNum();
                index +=num;
                Double oneAll = num * orderShopEntity.getPricecommodity();
                cartVo.setPricecommodityAll(oneAll);
                cartVo.setCmmodityName(orderShopEntity.getCmmodityName());
                cartVo.setPricecommodity(orderShopEntity.getPricecommodity());
                cartVo.setInTime(orderShopEntity.getInTime());
                cartVo.setNum(orderShopEntity.getNum());
                cartVo.setCommodityId(orderShopEntity.getCommodityId());
                cartVo.setMainImage(orderShopEntity.getMainImage());
                cartVo.setSubtitle(orderShopEntity.getSubtitle());
                cartVos.add(cartVo);
                all += oneAll;
            }
            mav.addObject("ordershop" , cartVos);
        }
        mav.addObject("allmoney" , all);
        mav.addObject("allindex" , index);
        Random random = new Random();
        Integer ab = random.nextInt(2000000000);
        String abc = String.valueOf(ab);
        String liushuihao = "1" +abc;
        mav.addObject("liushui" , liushuihao);

        return mav;
    }
    /**
     * 删除
     */
    @RequestMapping("/index2")
    public ModelAndView deleteindex2(ModelMap map){

        ModelAndView mav = new ModelAndView("ok");
      Object s= session.getAttribute("allq");
        Object sp=  session.getAttribute("spxx");
        System.out.println(s+"--"+sp);
        orderService.deletOrder();
        cartService.deleteShoppingAll();
        orderShopMapper.delorderall();

        List<CartEntity> shoppingEntities = new ArrayList<>();

        shoppingEntities = cartService.getShopping();

        List<ProductEntity> productEntityList = new ArrayList<>();
        productEntityList = productService.getCommodityEntity();
        mav.addObject("comds" , productEntityList);
        int i =0;
        for(ProductEntity productEntity : productEntityList){
            mav.addObject("comds" + i , productEntity);
            i+=1;
        }
        mav.addObject("comds" , productEntityList);

        mav.addObject("shoppings" , shoppingEntities);
       map.put("jiag",s);
        map.put("spm",sp);
        map.put("pd",0);
        return mav;
    }


    @GetMapping(value = "/indexdeleteshop")
    public ModelAndView indexdeleteshop() {

        ModelAndView mav = new ModelAndView("redirect:/index");
        try{
            cartService.deleteShoppingAll();
            orderShopMapper.delorderall();

        }catch (Exception e){
            log.info("错误日志:{}",e);

        }
        return mav;
    }


}
